<?php		

wp_enqueue_media();
wp_enqueue_script( 'ghostpool-gallery-field' );

echo '<div class="gp-gallery-field">';

	echo '<div class="gp-gallery-preview">';
	
		if ( is_array( $value ) ) {
			$value = implode( ',', $value );
		}
	
		if ( '' !== $value ) {

			$attachments = explode( ',', $value );
	
			foreach( $attachments as $attachment ) {
				if ( $attachment > 0 ) {
					$media_thumb = wp_get_attachment_image_src( $attachment, 'thumbnail' );
					$media_thumb = $media_thumb[0];
					echo '<img src="' . esc_url( $media_thumb ) . '" alt="" />';
				}	
			}	

		}	

	echo '</div>';

	$text = ( '' !== $value ) ? esc_html__( 'Edit Gallery', 'gpur' ) : esc_html__( 'Add Gallery', 'gpur' );

	echo '<input type="button" class="gp-media-button button button-primary" value="' . esc_attr( $text ) . '" data-add="' . esc_attr__( 'Add Gallery', 'gpur' ) . '" data-change="' . esc_attr__( 'Edit Gallery', 'gpur' ) . '" />';

	$show_class = ( '' !== $value ) ? ' gp-show' : '';
	
	echo '<a class="gp-remove-media-button' . $show_class . '" href="#">' . esc_html__( 'Remove Gallery', 'gpur' ) . '</a>';

	echo '<input type="hidden" class="gp-attachment-ids" name="' . esc_attr( $name ) . '" value="' . esc_attr( $value ) . '" />';

echo '</div>';